home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / frameobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  8.5 KB  |  351 lines

  1. /* Frame object implementation */
  2.  
  3. #include "Python.h"
  4.  
  5. #include "compile.h"
  6. #include "frameobject.h"
  7. #include "opcode.h"
  8. #include "structmember.h"
  9. #include "protos/frameobject.h"
  10.  
  11. #define OFF(x) offsetof(PyFrameObject, x)
  12.  
  13. static struct memberlist frame_memberlist[] = {
  14.     {"f_back",    T_OBJECT,    OFF(f_back),    RO},
  15.     {"f_code",    T_OBJECT,    OFF(f_code),    RO},
  16.     {"f_builtins",    T_OBJECT,    OFF(f_builtins),RO},
  17.     {"f_globals",    T_OBJECT,    OFF(f_globals),    RO},
  18.     {"f_locals",    T_OBJECT,    OFF(f_locals),    RO},
  19.     {"f_lasti",    T_INT,        OFF(f_lasti),    RO},
  20.     {"f_lineno",    T_INT,        OFF(f_lineno),    RO},
  21.     {"f_restricted",T_INT,        OFF(f_restricted),RO},
  22.     {"f_trace",    T_OBJECT,    OFF(f_trace)},
  23.     {"f_exc_type",    T_OBJECT,    OFF(f_exc_type)},
  24.     {"f_exc_value",    T_OBJECT,    OFF(f_exc_value)},
  25.     {"f_exc_traceback", T_OBJECT,    OFF(f_exc_traceback)},
  26.     {NULL}    /* Sentinel */
  27. };
  28.  
  29. static PyObject *
  30. frame_getattr(f, name)
  31.     PyFrameObject *f;
  32.     char *name;
  33. {
  34.     if (strcmp(name, "f_locals") == 0)
  35.         PyFrame_FastToLocals(f);
  36.     return PyMember_Get((char *)f, frame_memberlist, name);
  37. }
  38.  
  39. static int
  40. frame_setattr(f, name, value)
  41.     PyFrameObject *f;
  42.     char *name;
  43.     PyObject *value;
  44. {
  45.     return PyMember_Set((char *)f, frame_memberlist, name, value);
  46. }
  47.  
  48. /* Stack frames are allocated and deallocated at a considerable rate.
  49.    In an attempt to improve the speed of function calls, we maintain a
  50.    separate free list of stack frames (just like integers are
  51.    allocated in a special way -- see intobject.c).  When a stack frame
  52.    is on the free list, only the following members have a meaning:
  53.     ob_type        == &Frametype
  54.     f_back        next item on free list, or NULL
  55.     f_nlocals    number of locals
  56.     f_stacksize    size of value stack
  57.    Note that the value and block stacks are preserved -- this can save
  58.    another malloc() call or two (and two free() calls as well!).
  59.    Also note that, unlike for integers, each frame object is a
  60.    malloc'ed object in its own right -- it is only the actual calls to
  61.    malloc() that we are trying to save here, not the administration.
  62.    After all, while a typical program may make millions of calls, a
  63.    call depth of more than 20 or 30 is probably already exceptional
  64.    unless the program contains run-away recursion.  I hope.
  65. */
  66.  
  67. static PyFrameObject *free_list = NULL;
  68.  
  69. static void
  70. frame_dealloc(f)
  71.     PyFrameObject *f;
  72. {
  73.     int i;
  74.     PyObject **fastlocals;
  75.  
  76.     Py_TRASHCAN_SAFE_BEGIN(f)
  77.     /* Kill all local variables */
  78.     fastlocals = f->f_localsplus;
  79.     for (i = f->f_nlocals; --i >= 0; ++fastlocals) {
  80.         Py_XDECREF(*fastlocals);
  81.     }
  82.  
  83.     Py_XDECREF(f->f_back);
  84.     Py_XDECREF(f->f_code);
  85.     Py_XDECREF(f->f_builtins);
  86.     Py_XDECREF(f->f_globals);
  87.     Py_XDECREF(f->f_locals);
  88.     Py_XDECREF(f->f_trace);
  89.     Py_XDECREF(f->f_exc_type);
  90.     Py_XDECREF(f->f_exc_value);
  91.     Py_XDECREF(f->f_exc_traceback);
  92.     f->f_back = free_list;
  93.     free_list = f;
  94.     Py_TRASHCAN_SAFE_END(f)
  95. }
  96.  
  97. PyTypeObject PyFrame_Type = {
  98.     PyObject_HEAD_INIT(&PyType_Type)
  99.     0,
  100.     "frame",
  101.     sizeof(PyFrameObject),
  102.     0,
  103.     (destructor)frame_dealloc, /*tp_dealloc*/
  104.     0,        /*tp_print*/
  105.     (getattrfunc)frame_getattr, /*tp_getattr*/
  106.     (setattrfunc)frame_setattr, /*tp_setattr*/
  107.     0,        /*tp_compare*/
  108.     0,        /*tp_repr*/
  109.     0,        /*tp_as_number*/
  110.     0,        /*tp_as_sequence*/
  111.     0,        /*tp_as_mapping*/
  112. };
  113.  
  114. PyFrameObject *
  115. PyFrame_New(tstate, code, globals, locals)
  116.     PyThreadState *tstate;
  117.     PyCodeObject *code;
  118.     PyObject *globals;
  119.     PyObject *locals;
  120. {
  121.     PyFrameObject *back = tstate->frame;
  122.     static PyObject *builtin_object;
  123.     PyFrameObject *f;
  124.     PyObject *builtins;
  125.     int extras;
  126.  
  127.     if (builtin_object == NULL) {
  128.         builtin_object = PyString_InternFromString("__builtins__");
  129.         if (builtin_object == NULL)
  130.             return NULL;
  131.     }
  132.     if ((back != NULL && !PyFrame_Check(back)) ||
  133.         code == NULL || !PyCode_Check(code) ||
  134.         globals == NULL || !PyDict_Check(globals) ||
  135.         (locals != NULL && !PyDict_Check(locals))) {
  136.         PyErr_BadInternalCall();
  137.         return NULL;
  138.     }
  139.     extras = code->co_stacksize + code->co_nlocals;
  140.     if (back == NULL || back->f_globals != globals) {
  141.         builtins = PyDict_GetItem(globals, builtin_object);
  142.         if (builtins != NULL && PyModule_Check(builtins))
  143.             builtins = PyModule_GetDict(builtins);
  144.     }
  145.     else {
  146.         /* If we share the globals, we share the builtins.
  147.            Save a lookup and a call. */
  148.         builtins = back->f_builtins;
  149.     }
  150.     if (builtins != NULL && !PyDict_Check(builtins))
  151.         builtins = NULL;
  152.     if (free_list == NULL) {
  153.         /* PyObject_New is inlined */
  154.         f = (PyFrameObject *)
  155.             PyObject_MALLOC(sizeof(PyFrameObject) +
  156.                     extras*sizeof(PyObject *));
  157.         if (f == NULL)
  158.             return (PyFrameObject *)PyErr_NoMemory();
  159.         PyObject_INIT(f, &PyFrame_Type);
  160.     }
  161.     else {
  162.         f = free_list;
  163.         free_list = free_list->f_back;
  164.         if (f->f_nlocals + f->f_stacksize < extras) {
  165.             f = (PyFrameObject *)
  166.                 PyObject_REALLOC(f, sizeof(PyFrameObject) +
  167.                          extras*sizeof(PyObject *));
  168.             if (f == NULL)
  169.                 return (PyFrameObject *)PyErr_NoMemory();
  170.         }
  171.         else
  172.             extras = f->f_nlocals + f->f_stacksize;
  173.         PyObject_INIT(f, &PyFrame_Type);
  174.     }
  175.     if (builtins == NULL) {
  176.         /* No builtins!  Make up a minimal one. */
  177.         builtins = PyDict_New();
  178.         if (builtins == NULL || /* Give them 'None', at least. */
  179.             PyDict_SetItemString(builtins, "None", Py_None) < 0) {
  180.             Py_DECREF(f);
  181.             return NULL;
  182.         }
  183.     }
  184.     else
  185.         Py_XINCREF(builtins);
  186.     f->f_builtins = builtins;
  187.     Py_XINCREF(back);
  188.     f->f_back = back;
  189.     Py_INCREF(code);
  190.     f->f_code = code;
  191.     Py_INCREF(globals);
  192.     f->f_globals = globals;
  193.     if (code->co_flags & CO_NEWLOCALS) {
  194.         if (code->co_flags & CO_OPTIMIZED)
  195.             locals = NULL; /* Let fast_2_locals handle it */
  196.         else {
  197.             locals = PyDict_New();
  198.             if (locals == NULL) {
  199.                 Py_DECREF(f);
  200.                 return NULL;
  201.             }
  202.         }
  203.     }
  204.     else {
  205.         if (locals == NULL)
  206.             locals = globals;
  207.         Py_INCREF(locals);
  208.     }
  209.     f->f_locals = locals;
  210.     f->f_trace = NULL;
  211.     f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
  212.     f->f_tstate = tstate;
  213.  
  214.     f->f_lasti = 0;
  215.     f->f_lineno = code->co_firstlineno;
  216.     f->f_restricted = (builtins != tstate->interp->builtins);
  217.     f->f_iblock = 0;
  218.     f->f_nlocals = code->co_nlocals;
  219.     f->f_stacksize = extras - code->co_nlocals;
  220.  
  221.     while (--extras >= 0)
  222.         f->f_localsplus[extras] = NULL;
  223.  
  224.     f->f_valuestack = f->f_localsplus + f->f_nlocals;
  225.  
  226.     return f;
  227. }
  228.  
  229. /* Block management */
  230.  
  231. void
  232. PyFrame_BlockSetup(f, type, handler, level)
  233.     PyFrameObject *f;
  234.     int type;
  235.     int handler;
  236.     int level;
  237. {
  238.     PyTryBlock *b;
  239.     if (f->f_iblock >= CO_MAXBLOCKS)
  240.         Py_FatalError("XXX block stack overflow");
  241.     b = &f->f_blockstack[f->f_iblock++];
  242.     b->b_type = type;
  243.     b->b_level = level;
  244.     b->b_handler = handler;
  245. }
  246.  
  247. PyTryBlock *
  248. PyFrame_BlockPop(f)
  249.     PyFrameObject *f;
  250. {
  251.     PyTryBlock *b;
  252.     if (f->f_iblock <= 0)
  253.         Py_FatalError("XXX block stack underflow");
  254.     b = &f->f_blockstack[--f->f_iblock];
  255.     return b;
  256. }
  257.  
  258. /* Convert between "fast" version of locals and dictionary version */
  259.  
  260. void
  261. PyFrame_FastToLocals(f)
  262.     PyFrameObject *f;
  263. {
  264.     /* Merge fast locals into f->f_locals */
  265.     PyObject *locals, *map;
  266.     PyObject **fast;
  267.     PyObject *error_type, *error_value, *error_traceback;
  268.     int j;
  269.     if (f == NULL)
  270.         return;
  271.     locals = f->f_locals;
  272.     if (locals == NULL) {
  273.         locals = f->f_locals = PyDict_New();
  274.         if (locals == NULL) {
  275.             PyErr_Clear(); /* Can't report it :-( */
  276.             return;
  277.         }
  278.     }
  279.     if (f->f_nlocals == 0)
  280.         return;
  281.     map = f->f_code->co_varnames;
  282.     if (!PyDict_Check(locals) || !PyTuple_Check(map))
  283.         return;
  284.     PyErr_Fetch(&error_type, &error_value, &error_traceback);
  285.     fast = f->f_localsplus;
  286.     j = PyTuple_Size(map);
  287.     if (j > f->f_nlocals)
  288.         j = f->f_nlocals;
  289.     for (; --j >= 0; ) {
  290.         PyObject *key = PyTuple_GetItem(map, j);
  291.         PyObject *value = fast[j];
  292.         if (value == NULL) {
  293.             PyErr_Clear();
  294.             if (PyDict_DelItem(locals, key) != 0)
  295.                 PyErr_Clear();
  296.         }
  297.         else {
  298.             if (PyDict_SetItem(locals, key, value) != 0)
  299.                 PyErr_Clear();
  300.         }
  301.     }
  302.     PyErr_Restore(error_type, error_value, error_traceback);
  303. }
  304.  
  305. void
  306. PyFrame_LocalsToFast(f, clear)
  307.     PyFrameObject *f;
  308.     int clear;
  309. {
  310.     /* Merge f->f_locals into fast locals */
  311.     PyObject *locals, *map;
  312.     PyObject **fast;
  313.     PyObject *error_type, *error_value, *error_traceback;
  314.     int j;
  315.     if (f == NULL)
  316.         return;
  317.     locals = f->f_locals;
  318.     map = f->f_code->co_varnames;
  319.     if (locals == NULL || f->f_code->co_nlocals == 0)
  320.         return;
  321.     if (!PyDict_Check(locals) || !PyTuple_Check(map))
  322.         return;
  323.     PyErr_Fetch(&error_type, &error_value, &error_traceback);
  324.     fast = f->f_localsplus;
  325.     j = PyTuple_Size(map);
  326.     if (j > f->f_nlocals)
  327.         j = f->f_nlocals;
  328.     for (; --j >= 0; ) {
  329.         PyObject *key = PyTuple_GetItem(map, j);
  330.         PyObject *value = PyDict_GetItem(locals, key);
  331.         Py_XINCREF(value);
  332.         if (value != NULL || clear) {
  333.             Py_XDECREF(fast[j]);
  334.             fast[j] = value;
  335.         }
  336.     }
  337.     PyErr_Restore(error_type, error_value, error_traceback);
  338. }
  339.  
  340. /* Clear out the free list */
  341.  
  342. void
  343. PyFrame_Fini()
  344. {
  345.     while (free_list != NULL) {
  346.         PyFrameObject *f = free_list;
  347.         free_list = free_list->f_back;
  348.         PyObject_DEL(f);
  349.     }
  350. }
  351.